#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 13-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
using Thn.Interface.Vcl;
#endregion

namespace Demo
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG
    public
#else
    public
#endif
 partial class EditPage : Page
    {
        #region On Allow Navigation Away
        /// <summary>
        /// Checks to see if user can navigate away from this page.
        /// <para>If return false, this page should call ResumeLastNavigateRequest() to permit last request.</para>
        /// </summary>
        /// <returns>True if allowed, false otherwise.</returns>
        protected override bool OnAllowNavigationAway()
        {
            bool result = false;

            if (IsModified)
            {
                #region Page is modified, ask whether saving is neccessary

                //popup a dialog box to inform user
                DialogResult response = MessageBox.Show("Would you like to save your changes?", "Content modified", MessageBoxButtons.YesNoCancel);

                //determine what to do
                switch (response)
                {
                    #region Yes -> save then move away
                    case DialogResult.Yes:
                        SaveContent();
                        result = true;

                        //since this is a synchronous call, I don't need to invoke the following method
                        //ResumeLastNavigateRequest();
                        break;
                    #endregion

                    #region No -> Don't save, but allow to move away
                    case DialogResult.No:

                        result = true;
                        break;
                    #endregion

                    #region Cancel -> stay on this page
                    case DialogResult.Cancel:
                        result = false;
                        break;
                    #endregion

                    #region Unsupported
                    default:
                        throw new NotSupportedException(string.Format("OnInvalidate does not expect [{0}]", response));
                        break;
                    #endregion
                }
                #endregion
            }
            else
            {
                #region Page is not modified, allow navigation
                result = true;
                #endregion
            }

            return result;
        }
        #endregion

        #region Save Content
        void SaveContent()
        {
            //pretend that we already saved the data
            //by turning off the flag
            this.IsModified = false;
        }
        #endregion

        #region UI Events processing

        #region Is Modified Changed
        private void EditPage_IsModifiedChanged(object sender, EventArgs e)
        {
            bool saveRequired = this.IsModified;
            btnSave.Visible = saveRequired;
            lblModified.Visible = saveRequired;

            Log.Debug("IsModifiedChanged - value: {0}", saveRequired);
        }
        #endregion

        #region Save
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveContent();
        }
        #endregion

        #region Value Changed
        void ValueChanged(object sender, EventArgs e)
        {
            this.IsModified = true;
        }
        #endregion

        #region Enter Key
        private void EnterKey(object sender, EventArgs e)
        {
            //allows quick saving by pressing enter key anywhere
            if (IsModified) btnSave_Click(sender, e);
            #region Defensive tracing
            else
            {
                //no need to save when not modified
            }
            #endregion
        }
        #endregion

        #endregion

        #region On Load
        /// <summary>
        /// Initialize this form when form is loaded.
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            if (!DesignMode)
            {

            }
            #region Defensive tracing
            else
            {
                //don't do anything at design-time
            }
            #endregion

            //let my ancestor process its own logic
            base.OnLoad(e);
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public EditPage()
        {
            InitializeComponent();
        }
        #endregion

        private void lblGoHome_Click(object sender, EventArgs e)
        {
            Manager.Show("Login");
        }
    }
}
